﻿using System;
using System.Collections.Generic;
using T.FAS.Runtime.ScheduleJob.Dao;
using T.FAS.Runtime.ScheduleJob.SPI;

namespace T.FAS.Runtime.ScheduleJob.Manager
{
    /// <summary>
    /// 计划任务订阅管理
    /// </summary>
    public class ScheduleJobSubscriptionManager
    {
        private static readonly object lockObj = new object();
        private static ScheduleJobSubscriptionManager _manager = null;
        private ScheduleJobSubscriptionLogic logic = null;
        private ScheduleJobSubscriptionManager()
        {
            logic = new ScheduleJobSubscriptionLogic();
        }
        public static ScheduleJobSubscriptionManager GetInstance()
        {
            if (_manager == null)
            {
                lock (lockObj)
                {
                    if (_manager == null)
                        _manager = new ScheduleJobSubscriptionManager();
                }
            }
            return _manager;
        }
        public string CreateScheduleJobSubscription(ScheduleJobSubscription data)
        {
            if (string.IsNullOrEmpty(data.ID))
                data.ID = Guid.NewGuid().ToString();
            if (string.IsNullOrEmpty(data.Code))
                data.Code = DateTime.Now.ToString("yyyyMMddHHmmssfff");
            if (string.IsNullOrEmpty(data.OwnerID))
                data.OwnerID = UserUtil.GetCurrentUserID();
            if (string.IsNullOrEmpty(data.OwnerName))
                data.OwnerName = UserUtil.GetCurrentUserName();
            if (string.IsNullOrEmpty(data.TenantID))
                data.TenantID = UserUtil.GetCurrentUserTenantId();
            data.CreateTime = DateTime.Now;
            data.LastModifyTime = DateTime.Now;
            logic.AddScheduleJobSubscription(data);
            return data.ID;
        }
        public List<ScheduleJobSubscription> GetScheduleJobSubscriptionsByFilter(UserDefineQueryFilter filter)
        {
            return logic.GetScheduleJobSubscriptionsByFilter(filter);
        }
        public ScheduleJobSubscription GetScheduleJobSubscriptionById(string subId)
        {
            return logic.GetScheduleJobSubscriptionById(subId);
        }
        public void DeleteScheduleJobSubscription(string subId)
        {
            logic.DeleteScheduleJobSubscription(subId);
        }
    }
}
